using System;
using BRozklad.DBLib.DataModel;
using System.Collections;
using BRozklad.DBLib;

namespace BRozklad.RozkladLib
{
    class ReaderFullResult
    {
        DrawingDataArray data = new DrawingDataArray();
        ArrayList arrayPairs = new ArrayList();
        ArrayList arraySubGroupes = new ArrayList();

        private DrawingDataArray fillData()
        {
            WorkWithDB wwbd = new WorkWithDB();
            DrawingDataArray data = wwbd.FullResParaSelect();
            return data;
        }

        private string convertDayPairToStr(string day, int pair)
        {
            return day.ToString() + " " + pair.ToString();
        }
        private ArrayList getAllPairs(DrawingDataArray data)
        {
            ArrayList array = new ArrayList();
            Hashtable hash = new Hashtable();
            for (int i = 0; i < data.Count; i++)
            {
                DrawingDataRow row = (DrawingDataRow)data[i];
                string hashPair = convertDayPairToStr(row.timeDay, row.timePair);
                if (hash.Contains(hashPair) == false)
                {
                    array.Add(hashPair);
                    hash.Add(hashPair, i);
                }
            }
            array.Sort();
            return array;
        }
        private ArrayList getAllGroupes(DrawingDataArray data)
        {
            ArrayList array = new ArrayList();
            Hashtable hash = new Hashtable();
            for (int i = 0; i < data.Count; i++)
            {
                DrawingDataRow row = (DrawingDataRow)data[i];
                int idSubGroupe = row.smallGroupesSubID;
                int order = row.smallGroupesOrder;
                string input = order.ToString();
                for (int j = input.Length; j < 20; j++)
                {
                    input = "0" + input;
                }
                input += " " + idSubGroupe.ToString();
                if (hash.Contains(idSubGroupe) == false)
                {
                    array.Add(input);
                    hash.Add(idSubGroupe, i);
                }
            }
            array.Sort();
            for (int i = 0; i < array.Count; i++)
            {
                string s = (string)array[i];
                int spacePlace = s.IndexOf(' ') + 1;
                s = s.Substring(spacePlace);
                array[i] = s;
            }
            return array;
        }

        private Array getFullResult(DrawingDataArray data, ArrayList arrayPairs, ArrayList arraySubGroupes)
        {
            object[,] arr = new object[arrayPairs.Count * 2 + 1, arraySubGroupes.Count + 1];
            for (int j = 0; j < arraySubGroupes.Count; j++)
            {
                int idSubGroupe = Convert.ToInt32(arraySubGroupes[j]);
                for (int k = 0; k < data.Count; k++)
                {
                    DrawingDataRow row = (DrawingDataRow)data[k];
                    if (row.smallGroupesSubID == idSubGroupe)
                    {
                        arr[0, j + 1] = row.smallGroupesName;
                        break;
                    }
                }
            }
            for (int i = 0; i < arrayPairs.Count; i++)
            {
                string hashPair = (string)arrayPairs[i];
                arr[1 + i * 2, 0] = hashPair;
                for (int j = 0; j < arraySubGroupes.Count; j++)
                {
                    int idSubGrope = Convert.ToInt32(arraySubGroupes[j]);
                    if (isFullPair(data, hashPair, idSubGrope) == true)
                    {
                        arr[1 + i * 2, j + 1] = getIDForTimeAndGroupe(data, hashPair, idSubGrope, false);
                        arr[1 + i * 2 + 1, j + 1] = getIDForTimeAndGroupe(data, hashPair, idSubGrope, false);
                    }
                    else
                    {
                        if (arr[1 + i * 2, j + 1] == null)
                            arr[1 + i * 2, j + 1] = getIDForTimeAndGroupe(data, hashPair, idSubGrope, false);
                        int next = getIDForTimeAndGroupe(data, hashPair, idSubGrope, true);
                        if (next != -1)
                            arr[1 + i * 2 + 1, j + 1] = next;
                    }
                }
            }
            return arr;
        }


        private string getInfoForTimeAndGroupe(DrawingDataArray data, string hashPair, int idSubGrope)
        {
            string res = "";
            for (int i = 0; i < data.Count; i++)
            {
                DrawingDataRow row = (DrawingDataRow)data[i];
                if (idSubGrope == row.smallGroupesSubID)
                {
                    if (hashPair == convertDayPairToStr(row.timeDay, row.timePair))
                    {
                        res += row.subjectSubject + " " + row.teacherName + " " + row.auditoriasName;
                        //res += "/" + row.smallGroupesName;
                        return res;
                    }
                }
            }
            return res;
        }
        internal DrawingDataRow getInfoForPair(int index)
        {
            string res = "";
            if (index < 0)
                return null;
            int i = index;
            DrawingDataRow row = (DrawingDataRow)data[i];
            return row;
        }
        private int getIDForTimeAndGroupe(DrawingDataArray data, string hashPair, int idSubGrope, bool was)
        {
            for (int i = 0; i < data.Count; i++)
            {
                DrawingDataRow row = (DrawingDataRow)data[i];
                if (idSubGrope == row.smallGroupesSubID)
                {
                    if (hashPair == convertDayPairToStr(row.timeDay, row.timePair))
                    {
                        if (was == false)
                            return i;
                        was = false;
                    }
                }
            }
            return -1;
        }
        private bool isFullPair(DrawingDataArray data, string hashPair, int idSubGrope)
        {
            for (int i = 0; i < data.Count; i++)
            {
                DrawingDataRow row = (DrawingDataRow)data[i];
                if (idSubGrope == row.smallGroupesSubID)
                {
                    if (hashPair == convertDayPairToStr(row.timeDay, row.timePair))
                    {
                        return row.paraKilk > 0.75;
                    }
                }
            }
            return false;
        }

        internal Array readFullResult()
        {
            ReaderFullResult reader = new ReaderFullResult();

            data = reader.fillData();
            arrayPairs = reader.getAllPairs(data);
            arraySubGroupes = reader.getAllGroupes(data);
            Array fullResult = reader.getFullResult(data, arrayPairs, arraySubGroupes);

            return fullResult;
        }
    }
}
